package org.example.tests;

import org.example.demo.JavaBean;
import org.example.demo.ServiceBean;
import org.example.demo.SomeService;
import org.example.demo.SomeServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author zxiaoming
 * @Description:
 * @date 2021/2/2420:31
 */
public class TestDemo {
    public static void main(String[] args) {
        //Spring会把所有java对象转换为Bean进行生命周期的管理
        //2.先把需要管理的这些类转换为对应的bean进行统一管理（spring容器进行初始化）
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext1.xml");
        //3.获取容器中创建的类的实例
        System.out.println(context.getBean("someService"));
        //3.1这种方式获取实例
        SomeService someService = context.getBean("someService", SomeService.class);
        someService.doSomeThing();
        JavaBean javaBean = context.getBean("javaBean", JavaBean.class);
        System.out.println(javaBean);
        //4.如何配置和实现依赖注入[基于setter方法]
        ServiceBean serviceBean = context.getBean("serviceBean", ServiceBean.class);
        serviceBean.helloService();
        //5.基于构造函数完成依赖注入
        ServiceBean serviceBean_cto = context.getBean("serviceBean_cto", serviceBean.getClass());
        serviceBean_cto.helloService();
        ////////////////////////////////验证spring容器创建的实例是否是单例的////////////////
        //spring容器对需要管理的类默认使用单例创建
        //默认为singleton
        //可以更改为多例
        System.out.println(context.getBean("someService")==context.getBean("someService"));
    }
}
